package com.dragon.service.impl;

import com.dragon.dao.UserDao;
import com.dragon.dao.impl.UserDaoImpl;
import com.dragon.model.User;
import com.dragon.service.UserService;
import com.dragon.util.CodeUtil;
import com.dragon.util.MailUtil;

public class UserServiceImpl implements UserService {

	@Override
	public boolean doRegister(String userName, String password, String email) {
		if (!email.matches("^\\w+@(\\w+\\.)+\\w+$")) {
			return false;
		}
		String code = CodeUtil.generateUniqueCode();
		User user = new User(userName, email, password, 0, code);
		UserDao userDao = new UserDaoImpl();
		if (userDao.save(user) > 0) {
			new Thread(new MailUtil(email, code)).start();
			;
			return true;
		}
		return false;
	}

	@Override
	public boolean activeUser(String code) {
		UserDao userDao = new UserDaoImpl();
		if (userDao.activeUser(code) > 0) {
			return true;
		} else {
			return false;
		}
	}

}
